<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class laba_rugi extends Default_My_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function getTotalPenjualan($whr) {
        $query = "
            SELECT jkp.nama_jkp as jenis,sjkp.nama_sjkp as sub_jenis,sum(harga_jual_dp * quantity_dp) as total
            FROM kas_penjualan k JOIN sub_jenis_kas_penjualan sjkp ON k.id_sjkp = sjkp.id_sjkp
            JOIN jenis_kas_penjualan jkp ON jkp.id_jkp = sjkp.id_jkp
            WHERE true $whr
            GROUP BY jkp.nama_jkp,sjkp.nama_sjkp";
        $resultSet = $this->db->query($query);
        return $resultSet->result();
    }

    public function getDiskonPenjualan($whr) {
        return $this->get_scalar("
            SELECT sum(diskon_dp) as total
            FROM kas_penjualan k
            WHERE true $whr", "total");
    }

    public function getBebanPokok($whr) {
        $query = "
            SELECT concat('Beban Pokok ',jkp.nama_jkp) as jenis, sjkp.nama_sjkp as sub_jenis,sum(harga_beli_dp * quantity_dp) as total
            FROM kas_penjualan k JOIN sub_jenis_kas_penjualan sjkp ON k.id_sjkp = sjkp.id_sjkp
            JOIN jenis_kas_penjualan jkp ON jkp.id_jkp = sjkp.id_jkp
            WHERE true $whr
            GROUP BY jkp.nama_jkp,sjkp.nama_sjkp
                ";
        $resultSet = $this->db->query($query);
        return $resultSet->result();
    }
    
    public function getOperasional($whr) {
        $query = "
            SELECT jko.nama_jko  as jenis, sjko.nama_sjko as sub_jenis,sum(kas_keluar_ko) as total
            FROM kas_operasional k JOIN sub_jenis_kas_operasional sjko ON k.id_sjko = sjko.id_sjko
            JOIN jenis_kas_operasional jko ON jko.id_jko = sjko.id_jko
            WHERE (kas_keluar_ko > 0 or keterangan_ko = 'data_awal') $whr
            GROUP BY jko.nama_jko,sjko.nama_sjko
                ";
        $resultSet = $this->db->query($query);
        return $resultSet->result();
    }


}

?>